<template>
  <div class="create-post-page">
    <h4>新建文章</h4>
    <uploader
      action="/asdfasdf"
      class="d-flex align-items-center justify-content-center bg-light text-secondary w-100 my-4"
      :beforeUpload="uploadCheck"
      @file-uploaded="handleFileUploaded"
    >
      <!-- :uploaded="uploadedData" -->
      <template #default>
        <h2>点击上传头图</h2>
      </template>
      <template #loading>
        <div class="d-flex">
          <div class="spinner-border text-secondary" role="status">
            <span class="sr-only"></span>
          </div>
          <h2>正在上传</h2>
        </div>
      </template>
      <template #uploaded="dataProps">
        <div class="uploaded-area">
          {{dataProps.uploadedData}}
          <h3>点击重新上传</h3>
        </div>
      </template>
    </uploader>
    <validate-from @form-submit="onFormSubmit">
      <div class="mb-3">
        <label class="form-label" >文章标题</label>
        <validate-input
          placeholder="请输入文章标题"
          type="text"
          :rules="titleRules"
          v-model="titleVal"
        ></validate-input>
      </div>
      <div class="mb-3">
        <label class="form-label" >文章详情</label>
        <validate-input
          placeholder="请输入文章详情"
          type="text"
          :rules="contentRules"
          v-model="contentVal"
          :tag="'textarea'"
        ></validate-input>
      </div>
      <template #submit>
        <button class="btn btn-primary btn-large">发表文章</button>
      </template>
    </validate-from>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import ValidateFrom from '../components/ValidateFrom.vue'
import { useStore } from 'vuex'
import { GlobalDataProps, PostProps } from '../store'
import ValidateInput, { RulesProp } from '../components/ValidateInput.vue'
import Uploader from '../components/Uploader.vue'
import { useRouter } from 'vue-router'
import { beforeUploadCheck } from '../helper'
import createMessage from '../components/createMessage'
export default defineComponent({
  components: { ValidateFrom, ValidateInput, Uploader },
  setup () {
    const router = useRouter()
    const store = useStore<GlobalDataProps>()
    const titleRules: RulesProp = [
      { type: 'required', message: '文章标题不能为空' }
    ]
    const contentRules: RulesProp = [
      { type: 'required', message: '文章详情不能为空' }
    ]
    const titleVal = ref('')
    const contentVal = ref('')
    const imageId = ref('')
    const onFormSubmit = (result: boolean) => {
      if (result) {
        const { column, _id } = store.state.user
        if (column) {
          const newPost:PostProps = {
            title: titleVal.value,
            content: contentVal.value,
            column: column + '',
            author: _id
          }
          if (imageId.value) {
            newPost.image = '1'
          }
          store.dispatch('createPosts', { ...newPost, token: store.state.token }).then(res => {
            console.log(res)
            createMessage('创建成功，两秒后跳转到文章', 'success', 2000)
            setTimeout(() => {
              router.push(`/column/${column}`)
            }, 2000)
          })
        }
      }
    }
    const handleFileUploaded = (raw: any) => {
      if (raw) {
        imageId.value = raw
      }
    }
    const uploadCheck = (file: File) => {
      const result = beforeUploadCheck(file, { format: ['image/jpeg', 'image/png'], size: 1 })
      const { passed, error } = result
      if (error === 'format') {
        createMessage('上传图片只能是 JPG/PNG 格式!', 'error')
      }
      if (error === 'size') {
        createMessage('上传图片大小不能超过 1Mb', 'error')
      }
      return passed
    }
    return {
      titleRules,
      contentRules,
      titleVal,
      contentVal,
      onFormSubmit,
      uploadCheck,
      handleFileUploaded
    }
  }
})
</script>

<style>
.create-post-page .file-upload-container {
  height: 200px;
  cursor: pointer;
  overflow: hidden;
}
.create-post-page .file-upload-container img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.uploaded-area {
  position: relative;
}
.uploaded-area:hover h3 {
  display: block;
}
.uploaded-area h3 {
  display: none;
  position: absolute;
  color: #999;
  text-align: center;
  width: 100%;
  top: 50%;
}
</style>
